<?php

namespace App\Home\Requests;

use App\Admin\Models\SystemModel;
use App\Home\Models\User;
use Illuminate\Support\Arr;

class UserRequest extends FormRequest
{
    public function rules(SystemModel $systemModel, User $user)
    {
        $id = (int) optional($this->route('user'))->id;
        $rules = $systemModel->formValidationRules($user->getTable());
        if ($id) {
            if (isset($rules['phone'])) {
                $rules['phone'] = 'unique:users,phone,'.$id;
            }
        } else {
            if (isset($rules['phone'])) {
                $rules['phone'] = 'unique:users,phone';
            }
        }
        if ($this->isMethod('put')) {
            $rules = Arr::only($rules, $this->keys());
        }
        return $rules;
    }

    public function messages()
    {
        return [];
    }

    public function attributes()
    {
        $systemModel = new SystemModel();
        $user = new User();
        return $systemModel->fieldAttributes($user->getTable());
    }
}
